#include "my_demo.h"
#include "stdio.h"

//font思源黑体
LV_FONT_DECLARE(Source_SC);
LV_IMG_DECLARE(tiny);
#define USE_WEA_IMG 1
#if USE_WEA_IMG
//天气图片声明
LV_IMG_DECLARE(code_0);
LV_IMG_DECLARE(code_1);
LV_IMG_DECLARE(code_2);
LV_IMG_DECLARE(code_3);
LV_IMG_DECLARE(code_4);
LV_IMG_DECLARE(code_5);
LV_IMG_DECLARE(code_6);
LV_IMG_DECLARE(code_7);
LV_IMG_DECLARE(code_8);
LV_IMG_DECLARE(code_9);
LV_IMG_DECLARE(code_10);
LV_IMG_DECLARE(code_11);
LV_IMG_DECLARE(code_12);
LV_IMG_DECLARE(code_13);
LV_IMG_DECLARE(code_14);
LV_IMG_DECLARE(code_15);
LV_IMG_DECLARE(code_16);
LV_IMG_DECLARE(code_17);
LV_IMG_DECLARE(code_18);
LV_IMG_DECLARE(code_19);
LV_IMG_DECLARE(code_20);
LV_IMG_DECLARE(code_21);
LV_IMG_DECLARE(code_22);
LV_IMG_DECLARE(code_23);
LV_IMG_DECLARE(code_24);
LV_IMG_DECLARE(code_25);
LV_IMG_DECLARE(code_26);
LV_IMG_DECLARE(code_27);
LV_IMG_DECLARE(code_28);
LV_IMG_DECLARE(code_29);
LV_IMG_DECLARE(code_30);
LV_IMG_DECLARE(code_31);
LV_IMG_DECLARE(code_32);
LV_IMG_DECLARE(code_33);
LV_IMG_DECLARE(code_34);
LV_IMG_DECLARE(code_35);
LV_IMG_DECLARE(code_36);
LV_IMG_DECLARE(code_37);
LV_IMG_DECLARE(code_38);
LV_IMG_DECLARE(code_99);
#endif
lv_obj_t *scr_1;
lv_obj_t *label1_time;  //时间标签
lv_obj_t *label1_day;   //日期标签
lv_obj_t *label1_wea;   //天气标签
lv_obj_t *img_wea;      //天气图片
lv_obj_t *img_tiny;     //背景图片或纯色背景
lv_style_t label_style;
lv_style_t label_han_style;
lv_timer_t* my_lv_timer;

//存放日期和时间
int hour;
int minute;
int year;
int month;
int mday;
int wday;
int wea_code;

//动画制作函数
static void my_lv_anim_create(lv_obj_t *obj, lv_anim_exec_xcb_t exec_cb,  //操作对象，操作属性
                              int32_t start, int32_t end,                //开始值，结束值
                              uint32_t time, lv_anim_path_cb_t path_cb,  //操作时间，曲线函数
                              lv_anim_ready_cb_t ready_cb,              //动画结束，回调函数
                              uint32_t delay, uint16_t repeat_cnt, uint32_t repeat_delay      //延时，重复，重复延时
                              )
{
    lv_anim_t a;
    lv_anim_init(&a);
    lv_anim_set_var(&a, obj);
    lv_anim_set_values(&a, start, end);
    lv_anim_set_time(&a, time);
    lv_anim_set_exec_cb(&a, exec_cb);
    lv_anim_set_path_cb(&a, path_cb);
    lv_anim_set_ready_cb(&a,ready_cb);
    lv_anim_set_delay(&a,delay);
    if(repeat_cnt)
    {
        lv_anim_set_repeat_count(&a,repeat_cnt);
        lv_anim_set_repeat_delay(&a,repeat_delay);
    }
    lv_anim_start(&a);
}

//设置图片的透明度用函数，可用于动画
void my_img_set_opa(lv_obj_t *obj, lv_opa_t opa)
{
    lv_obj_set_style_img_opa(obj,opa,0);
    lv_obj_set_style_opa(obj,opa,0);
}

//设置图片的透明度用函数，可用于动画
void my_text_set_opa(lv_obj_t *obj, lv_opa_t opa)
{
    lv_obj_set_style_text_opa(obj,opa,0);
    lv_obj_set_style_opa(obj,opa,0);
}

//定时器任务回调函数
void my_timer_used(lv_timer_t* timer_u)
{
    static int i;
    char user_time[10];
    char user_date[30];
    char wdat[10];
    memset(user_time,0,sizeof(user_time));
    memset(user_date,0,sizeof(user_date));
    memset(wdat,0,sizeof(wdat));
    if(i%2==0){
        sprintf(user_time,"%02d:%02d",hour,minute);
    }
    else{
        sprintf(user_time,"%02d %02d",hour,minute);
    }
    switch(wday)
    {
        case 1: sprintf(wdat,"星期一"); break;
        case 2: sprintf(wdat,"星期二"); break;
        case 3: sprintf(wdat,"星期三"); break;
        case 4: sprintf(wdat,"星期四"); break;
        case 5: sprintf(wdat,"星期五"); break;
        case 6: sprintf(wdat,"星期六"); break;
        case 0: sprintf(wdat,"星期日"); break;
        default: memset(wdat,0,sizeof(wdat));  break;
    }
    sprintf(user_date,"%d/%02d/%02d %s",year+1900,month+1,mday,wdat);
    
    lv_label_set_text(label1_time,user_time);
    lv_label_set_text(label1_day,user_date);
}

//界面入口主函数
void my_demo_start(void)
{
    scr_1 = lv_scr_act();


    //时间标签label_style set
    lv_style_init(&label_style);
    lv_style_set_text_font(&label_style,&lv_font_montserrat_40);
    // lv_style_set_text_font(&label_style,&Source_SC);
    lv_style_set_text_color(&label_style,lv_color_white());

    //日期和天气标签 思源黑体20号字体
    lv_style_init(&label_han_style);
    // lv_style_set_text_font(&label_han_style,&lv_font_montserrat_36);
    lv_style_set_text_font(&label_han_style,&Source_SC);
    lv_style_set_text_color(&label_han_style,lv_color_white());
    
    //注释下面两行的代码就是图片背景，图片可以自设
    // img_tiny = lv_img_create(scr_1);
    // lv_img_set_src(img_tiny,&tiny);
    //使用纯色背景
    img_tiny = lv_obj_create(scr_1);
    lv_obj_set_style_bg_color(img_tiny,lv_color_black(),0);
    lv_obj_set_style_bg_opa(img_tiny,LV_OPA_COVER,0);
    lv_obj_set_pos(img_tiny,0,0);
    lv_obj_set_size(img_tiny,LV_HOR_RES,LV_VER_RES);

    #if USE_WEA_IMG
    img_wea = lv_img_create(img_tiny);
    lv_img_set_src(img_wea,&code_0);
    lv_obj_align(img_wea,LV_ALIGN_TOP_MID,0,10);
    #endif

    label1_time = lv_label_create(img_tiny);
    lv_obj_align(label1_time,LV_ALIGN_CENTER,0,0);
    lv_obj_add_style(label1_time,&label_style,LV_STATE_DEFAULT);
    lv_label_set_text(label1_time,"");

    label1_day = lv_label_create(img_tiny);
    lv_obj_align(label1_day,LV_ALIGN_CENTER,0,0);
    lv_obj_add_style(label1_day,&label_han_style,LV_STATE_DEFAULT);
    lv_label_set_text(label1_day,"");

    label1_wea = lv_label_create(img_tiny);
    lv_obj_align(label1_wea,LV_ALIGN_CENTER,0,0);
    lv_obj_add_style(label1_wea,&label_han_style,LV_STATE_DEFAULT);
    lv_label_set_text(label1_wea,"");
    

    my_lv_timer = lv_timer_create(my_timer_used,1000,NULL);
    lv_timer_enable(true); 

    // my_lv_anim_create(img_tiny, (lv_anim_exec_xcb_t) my_img_set_opa,  //操作对象，操作属性
    //                         0,  255,                //开始值，结束值
    //                         1000, lv_anim_path_ease_in_out,  //操作时间，曲线函数
    //                         NULL,              //动画结束，回调函数
    //                         0, 0, 0      //延时，重复，重复延时
    //                         );
    my_lv_anim_create(label1_day, (lv_anim_exec_xcb_t) lv_obj_set_y,  //操作对象，操作属性
                            280,  40,                //开始值，结束值
                            1000, lv_anim_path_ease_out,  //操作时间，曲线函数
                            NULL,              //动画结束，回调函数
                            1000, 0, 0      //延时，重复，重复延时
                            );
    my_lv_anim_create(label1_wea, (lv_anim_exec_xcb_t) lv_obj_set_y,  //操作对象，操作属性
                            300,  60,                //开始值，结束值
                            1000, lv_anim_path_ease_out,  //操作时间，曲线函数
                            NULL,              //动画结束，回调函数
                            1000, 0, 0      //延时，重复，重复延时
                            );
    my_lv_anim_create(label1_time, (lv_anim_exec_xcb_t) lv_obj_set_y,  //操作对象，操作属性
                            -140,  -25,                //开始值，结束值
                            1000, lv_anim_path_ease_out,  //操作时间，曲线函数
                            NULL,              //动画结束，回调函数
                            1000, 0, 0      //延时，重复，重复延时
                            );
}

#if USE_WEA_IMG
//天气动画回调函数
void wea_load_aim_cb(lv_anim_t *a)
{
    a->act_time = 0;
    switch(wea_code)
    {
        case 0: lv_img_set_src(img_wea,&code_0); break;
        case 1: lv_img_set_src(img_wea,&code_1); break;
        case 2: lv_img_set_src(img_wea,&code_2); break;
        case 3: lv_img_set_src(img_wea,&code_3); break;
        case 4: lv_img_set_src(img_wea,&code_4); break;
        case 5: lv_img_set_src(img_wea,&code_5); break;
        case 6: lv_img_set_src(img_wea,&code_6); break;
        case 7: lv_img_set_src(img_wea,&code_7); break;
        case 8: lv_img_set_src(img_wea,&code_8); break;
        case 9: lv_img_set_src(img_wea,&code_9); break;
        case 10:lv_img_set_src(img_wea,&code_10); break;
        case 11:lv_img_set_src(img_wea,&code_11); break;
        case 12:lv_img_set_src(img_wea,&code_12); break;
        case 13:lv_img_set_src(img_wea,&code_13); break;
        case 14:lv_img_set_src(img_wea,&code_14); break;
        case 15:lv_img_set_src(img_wea,&code_15); break;
        case 16:lv_img_set_src(img_wea,&code_16); break;
        case 17:lv_img_set_src(img_wea,&code_17); break;
        case 18:lv_img_set_src(img_wea,&code_18); break;
        case 19:lv_img_set_src(img_wea,&code_19); break;
        case 20:lv_img_set_src(img_wea,&code_20); break;
        case 21:lv_img_set_src(img_wea,&code_21); break;
        case 22:lv_img_set_src(img_wea,&code_22); break;
        case 23:lv_img_set_src(img_wea,&code_23); break;
        case 24:lv_img_set_src(img_wea,&code_24); break;
        case 25:lv_img_set_src(img_wea,&code_25); break;
        case 26:lv_img_set_src(img_wea,&code_26); break;
        case 27:lv_img_set_src(img_wea,&code_27); break;
        case 28:lv_img_set_src(img_wea,&code_28); break;
        case 29:lv_img_set_src(img_wea,&code_29); break;
        case 30:lv_img_set_src(img_wea,&code_30); break;
        case 31:lv_img_set_src(img_wea,&code_31); break;
        case 32:lv_img_set_src(img_wea,&code_32); break;
        case 33:lv_img_set_src(img_wea,&code_33); break;
        case 34:lv_img_set_src(img_wea,&code_34); break;
        case 35:lv_img_set_src(img_wea,&code_35); break;
        case 36:lv_img_set_src(img_wea,&code_36); break;
        case 37:lv_img_set_src(img_wea,&code_37); break;
        case 38:lv_img_set_src(img_wea,&code_38); break;
        default:lv_img_set_src(img_wea,&code_99); break;
    }
    lv_obj_align(img_wea,LV_ALIGN_TOP_MID,0,10);
    my_lv_anim_create(img_wea, (lv_anim_exec_xcb_t) my_img_set_opa,  //操作对象，操作属性
                            LV_OPA_0,  LV_OPA_100,                //开始值，结束值
                            1000, lv_anim_path_ease_in_out,  //操作时间，曲线函数
                            NULL,              //动画结束，回调函数
                            0, 0, 0      //延时，重复，重复延时
                            );
}
#endif

void demo_set_data_and_weatner(int m_year,int m_month,int m_mday,int m_hour,int m_minute,int m_wday,int code,char *wea)
{
    year = m_year;
    month = m_month;
    mday = m_mday;
    wday = m_wday;
    hour = m_hour;
    minute = m_minute;
    lv_label_set_text(label1_wea,wea);
    #if USE_WEA_IMG
    if(wea_code != code)
    {
        my_lv_anim_create(img_wea, (lv_anim_exec_xcb_t) my_img_set_opa,  //操作对象，操作属性
                            LV_OPA_100,  LV_OPA_0,                //开始值，结束值
                            1000, lv_anim_path_ease_in_out,  //操作时间，曲线函数
                            wea_load_aim_cb,              //动画结束，回调函数
                            0, 0, 0      //延时，重复，重复延时
                            );
        wea_code = code; 
    }
    #endif
}
